package com.faxon.leetcode.i;

import org.junit.Test;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-04-20 14:56
 * 83. 删除排序链表中的重复元素
 * 给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。
 * 示例 1:
 * 输入: 1->1->2
 * 输出: 1->2
 * 示例 2:
 * 输入: 1->1->2->3->3
 * 输出: 1->2->3
 **/
public class LeetCode83 {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode deleteDuplicates(ListNode head) {
        ListNode p = new ListNode(0);
        p.next = head;
        while (head != null) {
            if (head.next != null && head.val == head.next.val) {
                head.next = head.next.next;
            }else {
                head = head.next;
            }
        }
        return p.next;
    }

    @Test
    public void testDeleteDuplicates() {
        ListNode node = new ListNode(1);
        node.next = new ListNode(1);
        node.next.next = new ListNode(1);
        ListNode result = deleteDuplicates(node);
        System.out.println(result);
    }
}
